package com.mxpio.erp.inventory.controller;

import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.inventory.entity.BorrowApplyLine;
import com.mxpio.erp.common.inventory.service.BorrowApplyLineService;
import com.mxpio.erp.common.vo.InvFlowSummaryVo;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;

/**
 * @Author : wupp
 * @Datetime : 2022/9/15 10:16
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Tag(name = "BorrowApplyLineController", description = "借用归还行接口")
@RestController
@RequestMapping("/erp/inv/borrowapplyline/")
public class BorrowApplyLineController {
	@Autowired
	private BorrowApplyLineService borrowApplyLineService;
	@GetMapping("list")
    @Operation(summary = "借用归还行列表", description = "获取借用归还行列表（无分页）", method = "GET")
    public Result<List<BorrowApplyLine>> list(@Parameter(name="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<BorrowApplyLine> applyList = borrowApplyLineService.list(BorrowApplyLine.class, criteria);
        return Result.OK(applyList);
    }
	
    @GetMapping("page")
    @Operation(summary = "借用归还行列表", description = "借用归还行列表（分页）", method = "GET")
    public Result<Page<BorrowApplyLine>> page(@Parameter(name="查询参数") Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BorrowApplyLine> warehouseOrders = borrowApplyLineService.listPage(BorrowApplyLine.class, page, criteria);
        return Result.OK(warehouseOrders);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取借用归还行", description = "根据bizNo获取借用归还行", method = "GET")
    public Result<List<BorrowApplyLine>> getByParentId(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<BorrowApplyLine> applyLineList = borrowApplyLineService.list(BorrowApplyLine.class, c);
        if (applyLineList.size()>0){
            borrowApplyLineService.handleBorrowApplyLinesReturnedQuantity(applyLineList);//关联已归还数量
        }
        return Result.OK(applyLineList);
    }

    @GetMapping("executed/{bizNo}/{lineNo}")
    @Operation(summary = "获取借出批次", description = "获取借出批次", method = "GET")
    public Result<Collection<InvFlowSummaryVo>> getLot(
            @Parameter(description = "业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,
            @Parameter(description = "行号") @PathVariable(name = "lineNo", required = true) String lineNo,
            @Parameter(description = "仓库编号") @RequestParam(name = "whCode", required = true) String whCode) {
        Collection<InvFlowSummaryVo> flows = borrowApplyLineService.getExecutedReturnInvFlows(bizNo, lineNo, OrderEnums.AccessType.IN.getCode(), whCode);
        return Result.OK(flows);
    }

}
